package com.yc.sole.system.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import com.yc.sole.system.dto.DictDto;
import com.yc.sole.system.dto.DictItemDto;
import com.yc.sole.system.request.DictReq;
import com.yc.sole.system.repository.DictItemRepository;
import com.yc.sole.system.repository.DictRepository;
import com.yc.sole.system.request.DictQueryReq;
import com.yc.sole.system.response.DictItemResp;
import com.yc.sole.system.response.DictResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class DictBiz {

    private final DictRepository dictRepository;
    private final DictItemRepository dictItemRepository;

    /**
     * 新增
     *
     * @param dictReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveDict(DictReq dictReq) {
        DictDto dto = new DictDto();
        BeanUtil.copyProperties(dictReq, dto);
        dictRepository.saveDict(dto);
    }

    /**
     * 修改
     *
     * @param dictReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateDictById(DictReq dictReq) {
        DictDto dto = new DictDto();
        BeanUtil.copyProperties(dictReq, dto);
        dictRepository.updateDictById(dto);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteDict(Collection<Long> ids) {
        dictRepository.deleteDictByIds(ids);
        dictItemRepository.deleteDictItemByDictIds(ids);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param dictQueryReq
     * @return
     */
    public Paging<DictResp> selectDictPage(Paging<DictQueryReq> page, DictQueryReq dictQueryReq) {
        Paging<DictDto> paging = dictRepository.selectDictPage(page, dictQueryReq);
        return PageUtil.pageCopy(paging, DictResp.class);
    }

    /**
     * 查询所有字典
     *
     * @return
     */
    public Map<String, List<DictItemResp>> selectAllDict() {
        List<DictDto> dictDtos = dictRepository.selectAllDict();
        List<DictItemDto> dictItemDtos = dictItemRepository.selectAllDictItem();
        Map<Long, List<DictItemDto>> listMap = dictItemDtos.stream().collect(Collectors.groupingBy(DictItemDto::getDictId));
        Map<String, List<DictItemResp>> result = new HashMap<>(dictDtos.size());
        for (DictDto dictDto : dictDtos) {
            List<DictItemDto> itemDtos = listMap.get(dictDto.getId());
            if (CollUtil.isEmpty(itemDtos)) {
                itemDtos = new ArrayList<>();
            }
            List<DictItemDto> children = itemDtos.stream().sorted(Comparator.comparing(DictItemDto::getSort)).toList();
            List<DictItemResp> itemResps = BeanUtil.copyToList(children, DictItemResp.class);
            result.put(dictDto.getDictKey(), itemResps);
        }
        return result;
    }

    /**
     * 根据字典编码查询字典项
     *
     * @param dictKey
     * @return
     */
    public List<DictItemResp> selectByDictKey(String dictKey) {
        List<DictItemDto> dictItemDtos = dictItemRepository.selectDictItemByDictKey(dictKey);
        return BeanUtil.copyToList(dictItemDtos, DictItemResp.class);
    }

    /**
     * 通过Id查询字典
     *
     * @param dictId
     * @return
     */
    public DictResp selectDictById(Long dictId) {
        return BeanUtil.copyProperties(dictRepository.selectDictById(dictId), DictResp.class);
    }
}
